package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutResultContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 售货机微服务接受售货机终端出货消息
 */
@Component
@Topic(TopicConfig.VMS_RESULT_TOPIC)
public class VendoutResultMsgHandler implements MsgHandler {
    @Autowired
    private VendingMachineService vendingMachineService;


    @Override
    public void process(String jsonMsg) throws IOException {

        VendoutResultContract vendoutResultContract = JsonUtil.getByJson(jsonMsg, VendoutResultContract.class);
        if (StringUtils.isBlank(vendoutResultContract.getVendoutData().getOrderNo())) {
            throw new LogicException("终端响应信息有误");

        }
        //售货机微服务根据终端响应信息修改流水
        //处理出货逻辑
        vendingMachineService.vendoutResult(vendoutResultContract);


    }
}
